--
-- Constant: Arathi Basin Blacksmith
--
CattleProd_ARATHI_BASIN_BLACKSMITH = 1;

--
-- Constant: Arathi Basin Gold Mine
--
CattleProd_ARATHI_BASIN_GOLD_MINE = 3;

--
-- Constant: Arathi Basin Farm
--
CattleProd_ARATHI_BASIN_FARM = 2;

--
-- Constant: Arathi Basin Lumber Mill
--
CattleProd_ARATHI_BASIN_LUMBER_MILL = 4;

--
-- Constant: Arathi Basin Stables
--
CattleProd_ARATHI_BASIN_STABLES = 5;

--
-- Constant: Arathi Basin Scoreboard Column Bases Assaulted
--
CattleProd_ARATHI_BASIN_BASES_ASSAULTED = 1;

--
-- Constant: Arathi Basin Scoreboard Column Bases Defended
--
CattleProd_ARATHI_BASIN_BASES_DEFENDED = 2;

--
-- Arathi Basin Scoreboard Columns
--
CattleProd_ARATHI_BASIN_SCOREBOARD_COLUMNS = {
	CattleProd_ARATHI_BASIN_BASES_ASSAULTED,
	CattleProd_ARATHI_BASIN_BASES_DEFENDED
};

--
-- Setup Arathi Basin
--
function CattleProd_ArathiBasinSetup()
	CattleProd_Report(CattleProd_REPORT_LEVEL_MEDIUM,
		"Setup: %s", CattleProd_LocaleString(CattleProd_ZONE_ARATHI_BASIN));

	-- player scoreboard function
	CattleProd_GetBattlegroundScoreboardMap_Function = CattleProd_GetArathiBasinScoreboardMap;

	-- get defensible positions map
	CattleProd_GetBattlegroundDefensiblePositionsMap_Function = CattleProd_GetArathiBasinDefensiblePositionsMap;

	-- get battleground player scoreboard effort
	CattleProd_CalculateBattlegroundScoreboardEffort_Function = CattleProd_CalculateArathiBasinScoreboardEffort;

	-- get battleground defensive range
	CattleProd_GetBattlegroundDefensiveRange_Function = CattleProd_GetArathiBasinDefensiveRange;

	-- calculate battleground defender effort
	CattleProd_CalculateBattlegroundDefenderEffort_Function = CattleProd_CalculateArathiBasinDefenderEffort;
end

--
-- Get Arathi Basin scoreboard map.
--
-- playerIndex Player Index
--
-- return Player Scoreboard Map
--
function CattleProd_GetArathiBasinScoreboardMap(playerIndex)
	local scoreboard = {};
	
	for i = 1, # CattleProd_ARATHI_BASIN_SCOREBOARD_COLUMNS
	do
		local slotIndex = CattleProd_ARATHI_BASIN_SCOREBOARD_COLUMNS[i];
		local count = GetBattlefieldStatData(playerIndex, slotIndex);

		scoreboard[slotIndex] = count;
	end
	
	return scoreboard;
end

--
-- Calculate Arathi Basin Scoreboard Effort.
--
-- scoreboardDelta Scoreboard Delta
--
-- return Effort
--
function CattleProd_CalculateArathiBasinScoreboardEffort(scoreboardDelta)
	if (scoreboardDelta == nil)
	then
		CattleProd_Error("scoreboardDelta == nil");
		return nil;
	end
	if (type(scoreboardDelta) ~= "table")
	then
		CattleProd_Error("type(scoreboardDelta) == " .. type(scoreboardDelta));
		return nil;
	end

	local effortTotal = 0;

	-- bases assaulted
	local basesAssaulted = scoreboardDelta[CattleProd_ARATHI_BASIN_BASES_ASSAULTED];
	if (basesAssaulted == nil)
	then
		CattleProd_Error("basesAssaulted == nil");
	else
		local bonusEffort = CattleProd_GetSetting(
			CattleProd_ARATHI_BASIN_BASES_ASSAULTED_BONUS_EFFORT);

		local effort = basesAssaulted * bonusEffort;

		effortTotal = effortTotal + effort;
	end

	-- bases defended
	local basesDefended = scoreboardDelta[CattleProd_ARATHI_BASIN_BASES_DEFENDED];
	if (basesDefended == nil)
	then
		CattleProd_Error("basesDefended == nil");
	else
		local bonusEffort = CattleProd_GetSetting(
			CattleProd_ARATHI_BASIN_BASES_DEFENDED_BONUS_EFFORT);

		local effort = basesDefended * bonusEffort;

		effortTotal = effortTotal + effort;
	end

	-- effort total
	return effortTotal;
end

--
-- Arathi Basin Defensible Positions
--
local CattleProd_ArathiBasinDefensiblePositions = {
    CattleProd_ARATHI_BASIN_BLACKSMITH,
    CattleProd_ARATHI_BASIN_GOLD_MINE,
    CattleProd_ARATHI_BASIN_FARM,
    CattleProd_ARATHI_BASIN_LUMBER_MILL,
    CattleProd_ARATHI_BASIN_STABLES
}

--
-- Arathi Basin Defendable Locations
--
local CattleProd_ArathiBasinLocations = {
    [CattleProd_ARATHI_BASIN_BLACKSMITH] =  { 46.6, 45.1, 46.0, 44.0, 47.3, 47.2 },
    [CattleProd_ARATHI_BASIN_GOLD_MINE] =   { 57.5, 30.9, 55.9, 28.3, 59.0, 32.6, 58.1, 29.5 },
    [CattleProd_ARATHI_BASIN_FARM] =        { 56.0, 60.0, 54.7, 62.7, 57.3, 63.6, 55.7, 58.1 },
    [CattleProd_ARATHI_BASIN_LUMBER_MILL] = { 40.4, 55.7, 38.9, 58.4, 41.6, 59.3, 43.3, 61.0, 37.7, 53.5 },
    [CattleProd_ARATHI_BASIN_STABLES] =     { 37.3, 29.4, 36.0, 31.4, 35.4, 26.9 },
}

--
-- Get map of defensible positions.
--
-- return Defensible Position map
--
-- map: Position => { X, Y }
--
function CattleProd_GetArathiBasinDefensiblePositionsMap()
	local defensiblePositions = {};

	local defensibleStates = {};
	
	local faction = CattleProd_GetFaction();
	if (faction == 0)
	then
		table.insert(defensibleStates, 19); -- GM - a
		table.insert(defensibleStates, 20); -- GM - c
		table.insert(defensibleStates, 24); -- LM - a
		table.insert(defensibleStates, 25); -- LM - c
		table.insert(defensibleStates, 29); -- BS - a
		table.insert(defensibleStates, 30); -- BS - c
		table.insert(defensibleStates, 34); -- F  - a
		table.insert(defensibleStates, 35); -- F  - c
		table.insert(defensibleStates, 39); -- ST - a
		table.insert(defensibleStates, 40); -- ST - c
	elseif (faction == 1)
	then
		table.insert(defensibleStates, 17); -- GM - a
		table.insert(defensibleStates, 18); -- GM - c
		table.insert(defensibleStates, 22); -- LM - a
		table.insert(defensibleStates, 23); -- LM - c
		table.insert(defensibleStates, 27); -- BS - a
		table.insert(defensibleStates, 28); -- BS - c
		table.insert(defensibleStates, 32); -- F  - a
		table.insert(defensibleStates, 33); -- F  - c
		table.insert(defensibleStates, 37); -- ST - a
		table.insert(defensibleStates, 38); -- ST - c
	elseif (faction == nil)
	then
		CattleProd_Error("faction == nil");
		return defensiblePositions;
	else
		CattleProd_Error("faction == " .. tostring(faction));
		return defensiblePositions; 
	end
	
	for i, landmark in pairs(CattleProd_ArathiBasinDefensiblePositions)
	do
		-- lookup location
		local name, description, textureIndex, x, y
		name, description, textureIndex, x, y = GetMapLandmarkInfo(landmark);
		
		if (CattleProd_Contains(defensibleStates, textureIndex))
		then
			local locations = CattleProd_ArathiBasinLocations[landmark];
			if (locations == nil)
			then
				CattleProd_Error("landmark: " .. tostring(landmark) .. " locations: nil");
			else
				-- add locations to defensible positions
				table.insert(defensiblePositions, locations);
			end
		end
	end
	
	return defensiblePositions;
end

--
-- Get Arathi Basin Defensive Range.
--
-- return Defensive range
--
function CattleProd_GetArathiBasinDefensiveRange()
	return 2.75;
end


--
-- Calculate Arathi Basin defender bonus effort.
--
-- defenderCount Defender Count
--
-- return Effort
--
function CattleProd_CalculateArathiBasinDefenderEffort(defenderCount)
	if (defenderCount == nil)
	then
		CattleProd_Error("defenderCount == nil");
		return 0;
	end
	
	local defenderBonusEffort = CattleProd_GetSetting(CattleProd_ARATHI_BASIN_DEFENDER_BONUS_EFFORT);
	if (defenderBonusEffort == nil)
	then
		CattleProd_Error("defenderBonusEffort == nil");
		return 0;
	end

	return defenderCount * defenderBonusEffort;
end
